<template>
    <div class="chart-container">
        <div ref="chartRef" :style="{ width: '100%', height: '400px' }"></div>
    </div>



</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
const chartRef = ref(null)
let chartInstance = null
const xAxisData = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
// 监听窗口大小变化
const handleWindowResize = () => {
    if (chartInstance) {
        chartInstance.resize()
    }
}
// 初始化图表
const initChart = () => {
    if (!chartInstance) {
        chartInstance = echarts.init(chartRef.value)
        const option = {
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['expected', 'actual']
            },
            xAxis: {
                type: 'category',
                data: xAxisData
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: 'expected',
                    data: [820, 932, 901, 934, 1290, 1330, 1320],
                    type: 'line',
                    smooth: true,
                    itemStyle: { color: '#36a3f7' }
                },
                {
                    name: 'actual',
                    data: [420, 532, 501, 534, 890, 930, 920],
                    type: 'line',
                    smooth: true,
                    itemStyle: { color: '#f4516c' }
                }
            ]
        }
        option && chartInstance.setOption(option)
    }
}
// 挂载时初始化图表
onMounted(() => {
    initChart()
    window.addEventListener('resize', handleWindowResize)
})
onBeforeUnmount(() => {
    if (chartInstance) {
        chartInstance.dispose()
        chartInstance = null
    }
    window.removeEventListener('resize', handleWindowResize)
})
</script>

<style scoped>

</style>
